package com.tcode.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

import com.tcode.utils.ToolsConfig;

import java.io.IOException;

/**
 * 登录验证的过滤器
 */
@WebFilter("/*")
public class LoginFilter extends AbstractFilter {


    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
//        System.out.println(req);
        //0.强制转换
        HttpServletRequest request = (HttpServletRequest) req;
        //1.将在线人数统计放入session里
        request.getSession().setAttribute("onLineCount", ToolsConfig.getOnlineCount());
        //2.获取资源请求路径
        String uri = request.getRequestURI();
        //3.判断是否包含登录相关资源路径,要注意排除掉 css/js/图片/验证码等资源
        if(uri.contains("/index.jsp") || uri.contains("/InitServlet") || uri.contains("/css/") || uri.contains("/js/") || uri.contains("/img/")){
            //包含，用户就是想登录。放行
            chain.doFilter(req, resp);
        }else{
            //不包含，需要验证用户是否登录
            //4.从获取session中获取user
            Object user = request.getSession().getAttribute("user");
            if(user != null){
                //登录了。放行
                chain.doFilter(req, resp);
            }else{
                //没有登录。跳转登录页面
            	
                request.setAttribute("errorTip","您尚未登录，请登录");
                request.getRequestDispatcher("/index.jsp").forward(request,resp);
            }
        }

    }

    public void init(FilterConfig config) throws ServletException {
    	
    }

    public void destroy() {
    }

}
